﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using common.pages;

namespace Com.AlbLab.CR.Web.controls
{
    public partial class Upload : System.Web.UI.UserControl
    {
        public enum commandName
        {
            LOAD,
            SAVE,
            DELETE
        }

        public class fileArgs
        {
            public string id;
            public byte[] file;
            public string name;
        }

        public string Id
        {
            get { return hidFileId.Value;}
            set { hidFileId.Value=value;}
        }

        public string Filename
        {
            get { return lblNome.Text; }
            set { lblNome.Text = value; }
        }

        public delegate void FileEventHandler(commandName cmd,fileArgs file);
        public event FileEventHandler Actions;

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Page_DataBind(object sender, EventArgs e)
        {
            
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hidFileId.Value) || hidFileId.Value.Equals("0"))
            {
                ButtonErase.Visible = false;
                lblNome.Visible = false;
                btnDownLoad.Visible = false;
                ButtonUpload.Visible = true;
                FileUpload1.Visible = true;
            }
            else
            {
                ButtonErase.Visible = true;
                lblNome.Visible = true;
                btnDownLoad.Visible = true;
                ButtonUpload.Visible = false;
                FileUpload1.Visible = false;
            }
        }

        protected void btnDownLoad_OnClick(object sender, EventArgs e)
        {
            fileArgs args= new fileArgs();
            args.id=this.Id;

            if (Actions != null)
                Actions(commandName.LOAD, args);
            
            if (args.file == null)
            {
                ((BasePage)this.Page).info.AddMessage("Banca dati non consistente o non raggiungibile",GEP.Domain.LivelloMessaggio.ERROR );
            }
            else
            {
                    this.Response.Clear();
                    this.Response.ContentType = "file/pdf";
                    this.Response.AppendHeader("Content-Disposition", "attachment; filename=" + args.name);
                    this.Response.BinaryWrite(args.file);
                    this.Response.End();
            }
        }

        protected void ButtonUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.FileContent.Length == 0)
            {
                 ((BasePage)this.Page).info.AddMessage("Prima di proseguire occorre selezionareun file da caricare",GEP.Domain.LivelloMessaggio.ERROR );
                 return;
            }
             
            fileArgs args= new fileArgs();
            args.file = new byte[(int)FileUpload1.FileContent.Length];
            FileUpload1.FileContent.Read(args.file, 0, (int)FileUpload1.FileContent.Length);
            args.name = FileUpload1.PostedFile.FileName;
            if (Actions != null)
                Actions(commandName.SAVE, args);
            this.Filename = args.name;
            this.Id = args.id;
        }

        protected void ButtonErase_Click(object sender, EventArgs e)
        {
            fileArgs args = new fileArgs();
            args.id = this.Id;

            if (Actions != null)
                Actions(commandName.DELETE, args);
            this.Filename=args.name;
            this.Id = args.id;
           
        }   
    }
}